<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\VillageThings;
use App\Models\VillageThingsComment;
use App\Models\VillageThingsOperateLogs;
use App\Services\SensitiveWords;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class VillageThingsController extends Controller
{
    /*
     * 大事小情的列表
     * @param Request $request
     * @return bool
     */
    public function index(Request $request)
    {
        $pagesize  = $request -> input('pagesize') ?? 5;
        $region_id = $request -> input('region_id');
        $types     = $request -> input('types');
        $village_things_id  = $request -> input('village_thing_id');
        $user_id   = $request -> input('user_id');


        $village_things = VillageThings::with(['villager:id,user_name,real_name,head_portrait']) -> where('is_show','1') -> where(function($query)use($types){
            $types && $query -> where('types',$types);
        }) ->where(function ($query) use($region_id,$user_id){

            if($user_id){
                $query -> where('user_id', $user_id);
            }else{
                if($region_id){
                    $query -> where('region_id',$region_id);
                }else{
                    $query -> where('region_id',0);
                    $query -> orWhere('region_id',null);
                }
            }
        });

        if($village_things_id){

            $village_things  = $village_things ->where('id',$village_things_id)  -> orderBy('id','desc') -> get();
        }else{
            $village_things  = $village_things -> orderBy('id','desc') -> paginate($pagesize);
        }


        foreach($village_things as $k => $v){
            if($v -> villager -> real_name){
                $v -> villager -> user_name = $v -> villager -> real_name;
            }
            unset($v -> villager -> real_name);
            $v -> villager -> head_portrait_url = config('app.img_url').$v -> villager -> head_portrait;


            //最近点赞的用户
            $lastLogs = VillageThingsOperateLogs::where('village_things_id',$v -> id) -> where('operate_type','1') -> where('obj_id',0) ->orderBy('id','desc') -> first();
            if($lastLogs){
                $v -> last_zan_user = User::where('id',$lastLogs -> user_id) -> select(['id','user_name','real_name','head_portrait']) -> first();
                $v -> last_zan_user -> real_name && $v -> last_zan_user -> user_name = $v -> last_zan_user -> real_name;
                unset($v -> last_zan_user -> real_name);
                $v -> last_zan_user -> head_portrait_url = config('app.img_url') . $v -> last_zan_user -> head_portrait;
            }else{
                $v -> last_zan_user = null;
            }


            // 评论列表
            $v -> comment_list = VillageThingsComment::with(['commenter:id,user_name,real_name,head_portrait']) ->where('village_things_id',$v -> id) -> orderBy('id') -> get();
            foreach($v -> comment_list as $ko => $vo){

                if($vo -> commenter -> real_name){
                    $vo -> commenter -> user_name = $vo -> commenter -> real_name;
                }
                unset($vo -> commenter -> real_name);
                $vo -> commenter -> head_portrait_url = config('app.img_url').$vo -> commenter -> head_portrait;
                $comment_list[$ko] = $v;
            }
        }


        return jsonSuccess($village_things);
    }

    /*
     * 预约文章详情页
     * @return array
     */
    public function details(Request $request)
    {
        if($village_things_id = $request -> input('village_things_id')){

            // 一条大事小情数据
            $village_things = VillageThings::with(['villager:id,user_name,real_name,head_portrait']) -> where('id',$village_things_id) -> first();
            if($village_things){

                if($village_things -> villager){
                    if($village_things ->villager -> real_name){
                        $village_things ->villager -> user_name = $village_things ->villager -> real_name;
                    }
                    unset($village_things ->villager -> real_name);
                    $village_things -> villager -> head_portrait_url =  config('app.img_url').$village_things -> villager -> head_portrait;
                }


                // 评论列表
                $comment_list = VillageThingsComment::with(['commenter:id,user_name,real_name,head_portrait']) ->where('village_things_id',$village_things_id)->orderBy('zan_num','desc') -> orderBy('id','desc') -> get();
                foreach($comment_list as $k => $v){

                    if($v -> commenter -> real_name){
                        $v -> commenter -> user_name = $v -> commenter -> real_name;
                    }
                    unset($v -> commenter -> real_name);
                    $v -> commenter -> head_portrait_url = config('app.img_url').$v -> commenter -> head_portrait;
                    $comment_list[$k] = $v;
                }

                return jsonSuccess(['village_things' => $village_things,'comment_list' => $comment_list]);
            }else{
                return jsonError('',4000,404);
            }

        }else{
            return jsonSuccess();
        }
    }


    /*
     * 大事小情的添加
     * @param Request $request
     * @return string
     */
    public function addVillageThings(Request $request)
    {

        $param  = $request -> all();

        $message = [
            'content.required' => '请输入内容',
            'region_id.required' => '请选择地区'
        ];
        $validator = Validator::make($param,[
            'content' => 'required',
            'region_id' => 'required'
        ],$message);

        // 验证失败返回
        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }


        $bad_word = SensitiveWords::getBadWord($request -> input('content'));
        if (!empty($bad_word)) {
            return jsonError('请勿使用敏感词：'.current($bad_word));
        }

        if(!$param['imgs'] && !$param['video']){
            return jsonError('请添加图片、视频的一种');
        }


        $village_things = VillageThings::store($param);
        if($village_things){
            return jsonSuccess(2007);
        }else{
            return jsonError();
        }


    }


    /*
     * 预约评论
     * @return bool
     */
    public function comment(Request $request)
    {
        if(!$request -> filled('village_things_id') || !is_numeric($request -> input('village_things_id'))){
            return jsonError(4020);
        }

        if(!$request -> filled('content')){
            return jsonError(4036);
        }

        // 对敏感词过滤
        $bad_word = SensitiveWords::getBadWord($request -> input('content'));
        if (!empty($bad_word)) {
            return jsonError('请勿使用敏感词：'.current($bad_word));
        }

        $param = $request -> all();

        // 判断自己是否评论自己，自己回复自己
        /*$check = VillageThingsComment::check($request -> input('village_things_id'), $request -> input('reply_id'));
        if($check){
            return jsonError(4039);
        }*/



        $res = VillageThingsComment::store($param);
        if($res){
            //操作记录
            VillageThingsOperateLogs::operateLog($param['village_things_id'],4,$res -> id);
            return jsonSuccess(2007);
        }else{
            return jsonError();
        }
    }


    /*
     * 预约操作表 operate_type: 点赞1 分享2，3关注
     * @return string
     */
    public function VillageThingsOperateLogs(Request $request)
    {
        //验证
        if(!is_numeric($request -> input('operate_type')) || !$request -> input('operate_type')){
            return jsonError(4020);
        }

        // 大事小情id存在, 评论id不存在，对大事小情评论
        // 评论id存在，大事小情id存在，优先 对 评论 进行回复
        if($request -> input('village_things_id')){
            if(!is_numeric($request -> input('village_things_id'))){
                return jsonError(4020);
            }

            if(!VillageThings::find($request -> input('village_things_id'))){
                return jsonError('',4000,404);
            }
        }
        $village_things_id = $request -> input('village_things_id');
        if($request -> input('comment_id')){
            if(!is_numeric($request -> input('comment_id'))){
                return jsonError(4020);
            }

            if(!VillageThingsComment::find($request -> input('comment_id'))){
                return jsonError('',4000,404);
            }

            if($village_things_id){
                $village_things_id = VillageThingsComment::where('id',$request -> input('comment_id')) ->value('village_things_id');
            }
        }

        $operate_type   = $request -> input('operate_type');

        // 对大事小情操纵
        $res = VillageThingsOperateLogs::operateLog($village_things_id,$operate_type,($request -> input('comment_id')?? 0));
        if($res){
            return jsonSuccess(2007);
        }else{
            return jsonError();
        }

    }

    /*
     * 查看更多评论
     * @return string
     */
    public function commentMore(Request $request)
    {
        $village_things_id = $request -> input('village_things_id');

        $commentMore = VillageThingsComment::with(['commenter:id,user_name,real_name,head_portrait']) ->where('village_things_id',$village_things_id)->orderBy('zan_num','desc') -> orderBy('id','desc') -> get();

        foreach($commentMore as $k => $v){

            if($v -> commenter -> real_name){
                $v -> commenter -> user_name = $v -> commenter -> real_name;
            }
            unset($v -> commenter -> real_name);
            $v -> commenter -> head_portrait_url = config('app.img_url').$v -> commenter -> head_portrait;
            $commentMore[$k] = $v;
        }

        return jsonSuccess($commentMore);

    }
}
